package com.ifast.asset.quartz.Controller;

import com.ifast.asset.quartz.StartOfDayJob;
import com.ifast.asset.quartz.pojo.SchJob;
import com.ifast.asset.quartz.service.JobService;
import com.ifast.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/nmap")
public class JobController {

    @Autowired
    private JobService jobService;
    @Value("${auditUrl}")
    String auditUrl;
    //获取所有定时任务列表
    @GetMapping("/getAllJob")
    @ResponseBody
    public Result getAllJob() {

        return Result.ok(jobService.getAllJob());
    }

    //添加定时任务
    @PostMapping("/addSchJob")
    @ResponseBody
    public  Result addSchJob(@RequestBody SchJob schJob) {

        Class c2 = StartOfDayJob.class;
        jobService.addJob(schJob.getJobName(),"testuser",c2,schJob.getCorn(),schJob.getCode());
        return Result.ok();
    }

    /**
     * 创建cron任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cron",method = RequestMethod.GET)
    public Result startCronJob( String jobName,  String jobGroup){
        //jobService.addJob(jobName,jobGroup);
        return Result.ok();
    }

    /**
     * 创建异步任务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/async",method = RequestMethod.GET)
    public Result startAsyncJob( String jobName,  String jobGroup){
        //jobService.addAsyncJob(jobName,jobGroup);
        return Result.ok();
    }

    /**
     * 暂停任务
     * @param jobName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pause",method = RequestMethod.GET)
    public Result pauseJob(String jobName){
        String jobGroup = "testuser";
        jobService.pauseJob(jobName,jobGroup);
        return Result.ok();
    }

    /**
     * 恢复任务
     * @param jobName
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/resume",method = RequestMethod.GET)
    public Result resumeJob( String jobName){
        String jobGroup = "testuser";
        jobService.resumeJob(jobName,jobGroup);
        return Result.ok();
    }

    /**
     * 删除务
     * @param jobName
     * @param jobGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.PUT)
    public Result<Object> deleteJob(@RequestParam("jobName") String jobName, @RequestParam("jobGroup") String jobGroup){
        jobService.deleteJob(jobName,jobGroup);
        return Result.ok();
    }

    /**
     * 删除务
     * @param jobName
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteJobByGet",method = RequestMethod.GET)
    public Result<Object> deleteJobByGet( String jobName){
        String jobGroup = "testuser";
        jobService.deleteJob(jobName,jobGroup);
        return Result.ok();
    }
}
